package com.android.launcher3.setting;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.launcher3.Utilities;
import com.github.lzyzsd.circleprogress.DonutProgress;
import com.android.launcher3.BaseActivity;
import com.android.launcher3.R;
import com.x.component.hardware.ProcStatsData;
import com.x.component.hardware.SocComponent;
import com.x.component.utils.BridgeSystemProperties;

public class LauncherAboutActivity extends BaseActivity {

    private static final String TAG = "LauncherAboutActivity";

    private TextView tv_brand;
    private TextView tv_display;
    private TextView tv_ram;
    private TextView tv_rom;
    private TextView tv_systemVersionInfo;
    private TextView tv_protocolVersionInfo;
    private TextView tv_androidVersionInfo;
    private TextView tv_androidSNInfo;

    private DonutProgress donut_ram;
    private DonutProgress donut_rom;

    private LinearLayout layoutContainer;
    private View icBackToHome;

    private LinearLayout ll_android;
    private LinearLayout ll_SN;
    private LinearLayout ll_version;
    private LinearLayout ll_hat;
    private LinearLayout ll_rom;
    private LinearLayout ll_ram;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        setContentView(R.layout.launcher_about_activity);
        setUpView();
    }

    private void setUpView() {
        layoutContainer = findViewById(R.id.layout_container);
        icBackToHome = findViewById(R.id.ic_back_to_home);
        ll_android = findViewById(R.id.ll_android);
        ll_SN = findViewById(R.id.ll_SN);
        ll_version = findViewById(R.id.ll_version);
        ll_hat = findViewById(R.id.ll_hat);
        ll_rom = findViewById(R.id.ll_rom);
        ll_ram = findViewById(R.id.ll_ram);

        findViewById(R.id.ic_back_to_home).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent home = new Intent(Intent.ACTION_MAIN);
//                home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                home.addCategory(Intent.CATEGORY_HOME);
//                LauncherAboutActivity.this.startActivity(home);
                finish();
            }
        });

//        findViewById(R.id.iv_head_back).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                LauncherAboutActivity.this.finish();
//            }
//        });

        tv_brand = findViewById(R.id.tv_brand);
        tv_display = findViewById(R.id.tv_display);
        tv_ram = findViewById(R.id.tv_ram);
        tv_rom = findViewById(R.id.tv_rom);
        tv_systemVersionInfo = findViewById(R.id.tv_systemVersionInfo);
        tv_protocolVersionInfo = findViewById(R.id.tv_protocolVersionInfo);
        tv_androidVersionInfo = findViewById(R.id.tv_androidVersionInfo);
        tv_androidSNInfo = findViewById(R.id.tv_androidSNInfo);

        donut_ram = findViewById(R.id.donut_ram);
        donut_rom = findViewById(R.id.donut_rom);

        String brand = BridgeSystemProperties.getSystemProperty("persist.sys.oem.brand", "ApplePie");
        String width = BridgeSystemProperties.getSystemProperty("persist.panel.width", "1920");
        String height = BridgeSystemProperties.getSystemProperty("persist.panel.height", "720");
        String protocolVersion = BridgeSystemProperties.getSystemProperty("zj.zsender.version", "");
        String systemVersionInfo = BridgeSystemProperties.readSystemVersionInfo();
        String systemSN = BridgeSystemProperties.readSystemSerialNumber();
        tv_display.setText(width + "x" + height + "@30fps");
        tv_brand.setText(brand);
        tv_protocolVersionInfo.setText(protocolVersion);
        tv_systemVersionInfo.setText(systemVersionInfo);
        tv_androidSNInfo.setText(systemSN);

        SocComponent INSTANCE = SocComponent.getInstance();
        INSTANCE.init(getApplicationContext());
        ProcStatsData.MemInfo meminfo = INSTANCE.getCpuRamInfo();
        double realTotalRam = meminfo.realTotalRam;
        double realFreeRam = meminfo.realFreeRam;
        double realUsedRam = meminfo.realUsedRam;
        final double bytesPerGB = 1024 * 1024 * 1024;
        float freeRam = (float) (realFreeRam / bytesPerGB);
        int totalRam = (int) Math.round((realTotalRam / bytesPerGB));

        int ramProgress = (int) ((freeRam * 100) / totalRam);
        donut_ram.setProgress(ramProgress);

        ProcStatsData.RomInfo rominfo = INSTANCE.getDeviceRomInfo();
        tv_ram.setText(meminfo.uiFreeRam + " / " + meminfo.uiTotalRam + " GB");

        int totalRom = Integer.parseInt(rominfo.totalRom);
        int freeRom = Integer.parseInt(rominfo.freeRom);

        int romProgress = (int) ((freeRom * 100) / totalRom);
        donut_rom.setProgress(romProgress);
        tv_rom.setText(rominfo.freeRom + " / " + rominfo.totalRom + " GB");

        screenAdaptation();

    }


    private void screenAdaptation() {


        layoutContainer.post(() -> {

            DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
            if (Utilities.isLongStripScreen(getApplicationContext())) {
//                LinearLayout.LayoutParams gapLayoutParams = new LinearLayout.LayoutParams(Utilities.pxFromDp(10, displayMetrics), Utilities.pxFromDp(10, getResources().getDisplayMetrics()));
//                findViewById(R.id.gap1).setLayoutParams(gapLayoutParams);

                LinearLayout.LayoutParams icBackToHomeLayoutParams = (LinearLayout.LayoutParams) icBackToHome.getLayoutParams();

                icBackToHomeLayoutParams.width = Utilities.pxFromDp(97.5f, displayMetrics);
                icBackToHomeLayoutParams.height = Utilities.pxFromDp(60, displayMetrics);
                icBackToHome.setLayoutParams(icBackToHomeLayoutParams);

                ll_android.setBackgroundResource(R.drawable.bg_about_single_small);
                ll_SN.setBackgroundResource(R.drawable.bg_about_single_small);
                ll_version.setBackgroundResource(R.drawable.bg_about_single_small);
                ll_hat.setBackgroundResource(R.drawable.bg_about_single_small);
                ll_ram.setBackgroundResource(R.drawable.bg_about_single_small);
                ll_rom.setBackgroundResource(R.drawable.bg_about_single_small);

            }
        });


    }
}
